<?php

class StructureController extends Crb_Controller
{
	
    public function preDispatch()
    {
        parent::preDispatch();
    }
	public function indexAction()
	{
$this->view->layout()->setLayout('admin');
        
	}
	
	public function testAction()
	{
	    $this->setNoRender();
		$this->disableLayout();
		
	    $guidDAO = new Menu_Mediator();
	   echo  $guidDAO->perform('f6046d3fd1947a979abd79665e40c6f2');
	    
	    
	}
	
	public function operationsAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$Request = $this->getRequest();
		
		$operation = $Request->getParam('operation');
		
		$structureDAO = new Structure();
		$Response = array('status'=>1,'description' => '');
		try {
    		switch ($operation)
    		{
    		    
    		    case 'get_children':
    		        echo $this->_helper->json($structureDAO->buildJsTree( $structureDAO->buildHierarchy() ));
    		        break;
    		    case 'create_node':
                    $position = $Request->getParam('id');
                    $title = $Request->getParam('title');
                    $newID = $structureDAO->add($position, $title);
                    $Response['id'] = $newID; 
    		        break;
    		    case 'rename_node':
                    $position = $Request->getParam('id');
                    $title = $Request->getParam('title');
                    $structureDAO->rename($position, $title);
    		        break;
    		    case 'remove_node':
                    $id = $Request->getParam('id');
                    $structureDAO->delete($id);
    		        break;
    		    case 'move_node':
    		        $copy     = $Request->getParam('copy',0);
    		        $parentId = $Request->getParam('ref',0);
    		        $position = $Request->getParam('position',0);
                    $id       = $Request->getParam('id');
                    
                    $structureDAO->move($id,$parentId);
    		        break;

    		}
		} catch (Exception $e) {
		    $Response['description'] = $e->getMessage(); 
		    $Response['status'] = 0;
		}
		echo $this->_helper->json($Response);
		
		
	} 
	
	
	public function getMenuToolAction()
	{
	    		$this->setNoRender();
		        $this->disableLayout();
		        	    $guidDAO = new Menu_Mediator();
	   echo  $guidDAO->perform($this->getRequest()->getParam('guid'));
	} 
}

